<?php
/*
 * @Author: sunkaiyuan sunkaiyuan@namenode.com
 * @Date: 2022-10-05 23:16:26
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-07-25 15:01:34
 * @Description: 
 */

namespace App\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;

class Event
    {
    use SerializesModels;
    public $token;
    public $listeners = [];
    public $data;
    public $router;


    public function __construct($data, $router = null, $token = null)
        {
        $this->data = $data;

        $this->token = $token;

        $this->router = $router;
        $this->setListen();
        if (method_exists($this, 'boot')) {
            App::call([$this, 'boot']);
            }
        }
    public function setListen()
        {
        //监听自定义订阅
        $events = app('events');
        request()->merge(['token' => $this->token]);
        foreach ($this->listeners as $listener) {
            App::call([$events, "listen"], ['events' => $this::class, 'listener' => $listener]);
            //TNND会出警告
            // $events->listen($this::class, $listener);
            }
        return $this;
        }
    }